#include "itemManager.h"

itemManager* itemManager::_instance = 0;

itemManager* itemManager::Instance()
{
	if (_instance == NULL)
	{
		_instance = new itemManager();
	}
	return _instance;
}

itemManager::itemManager()
{
	for (int i = 0; i < 100; i++)
	{
		freeMem.push_back(new Item());
	}
}

void itemManager::updata()
{
	//cout << freeMem.size() << " " << manager.size() << endl;
	for (list<Item*>::iterator itor = manager.begin();
		itor != manager.end();)
	{
		if ((*itor)->checkRemove())
		{
			/*delete *itor;
			itor = manager.erase(itor);*/
			list<Item*>::iterator itor1 = itor;
			itor1++;
			freeMem.splice(freeMem.begin(), manager, itor);
			itor = itor1;
		}else
		{
			(*itor)->updata();
			itor++;
		}
	}
}

void itemManager::draw()
{
	for (list<Item*>::iterator itor = manager.begin();
		itor != manager.end(); itor++)
	{
		(*itor)->draw();
	}
}

/*
void itemManager::add(Item* b)
{
	manager.push_back(b);
}*/

void itemManager::add(stdItem std, Point p)
{
	list<Item*>::iterator itor = freeMem.begin();
	(*itor)->setInformation(std, p);
	manager.splice(manager.begin(), freeMem, itor);
}

void itemManager::checkHit(Player& p)
{
	for (list<Item*>::iterator itor = manager.begin();
		itor != manager.end(); itor++)
	{
		if(p.checkHit((*itor)->getNowPoint(), (*itor)->getRange(), 0))
		{
			(*itor)->changeRemove();
		}
	}
}

void itemManager::clear()
{
	freeMem.splice(freeMem.begin(), manager);
}